<?php

function get_customuser_array($add_blank=true, $status="Active", $assigned_user="", $use_real_name=false, $user_name_begins = null, $is_group=' AND portal_only=0 ', $from_cache = true) {
	global $locale;
	global $sugar_config;

	if(empty($locale)) {

		$locale = new Localization();
	}
	if($from_cache)
		$user_array = get_register_value('user_array', $add_blank. $status . $assigned_user);

	if(!isset($user_array)) {
		$db = DBManagerFactory::getInstance();
		$temp_result = Array();
		// Including deleted users for now.
		if (empty($status)) {
			$query = "SELECT id, first_name, last_name, user_name from users WHERE 1=1".$is_group;
		}
		else {
			$query = "SELECT id, first_name, last_name, user_name from users WHERE status='$status'".$is_group;
		}

		if (!empty($user_name_begins)) {
			$query .= " AND user_name LIKE '$user_name_begins%' ";
			
			/* BEGIN - SECURITY GROUPS */
			global $current_user, $sugar_config;
			if(!is_admin($current_user)
				&& isset($sugar_config['securitysuite_filter_user_list'])
				&& $sugar_config['securitysuite_filter_user_list'] == true
			) {
				require_once('modules/SecurityGroups/SecurityGroup.php');
				global $current_user;
				$group_where = SecurityGroup::getGroupUsersWhere($current_user->id);
				$query .= " AND (".$group_where.") ";
			}
			/* END - SECURITY GROUPS */
		}
		if (!empty($assigned_user)) {
			$query .= " OR id='$assigned_user'";
		}
		$query = $query.' ORDER BY user_name ASC';
		$GLOBALS['log']->debug("get_user_array query: $query");
		$result = $db->query($query, true, "Error filling in user array: ");

		if ($add_blank==true) {
			// Add in a blank row
			$temp_result[''] = '';
		}

		// Get the id and the name.
		while($row = $db->fetchByAssoc($result)) {
			if($use_real_name == true || showFullName()) {
					$temp_result[$row['id']] = $row['first_name']."  ".$row['last_name'];
			} else {
				$temp_result[$row['id']] = $row['first_name'].$row['last_name'];
			}
		}

		$user_array = $temp_result;
		if($from_cache)
			set_register_value('user_array', $add_blank. $status . $assigned_user, $temp_result);
	}

	asort($user_array);
	return $user_array;
}


?>
